import numpy as np
import matplotlib.pyplot as plot
from matplotlib import pyplot as plt


def np_test():
     # 1. 创建一维数组
     a=np.array([1,2,3,4,5])
     print("一维数组 a= ",a)

#      2. 创建二维数组
     b=np.array([[1,2,3],[4,5,6]])
     print("\n二维数组 b=\n",b)
#      3. 快速生成数组
     print("\nnp.zeros(3) =",np.zeros(3))     # 全0向量
     print("\nnp.ones(2,3) =\n",np.ones((2,3)))  # 全1矩阵
     print("\nnp.arange(0,10,2) =",np.arange(0,10,2))  # 等差数列
     print("\nnp.linspace(0,1,5) =",np.linspace(0,1,5))  # 均分区间

#      4. 基本运算
     x=np.array([1,2,3])
     y=np.array([4,5,6])
     print("\nx + y = \n",x+y)
     print("x * y = ",x*y)
     print("x^2 =",x**2)
     print("x.dot(y) = ",x.dot(y))

#      5. 数组属性
     print("\n数组 b 的形状:",b.shape)
     print("数组 b 的维度:",b.ndim)
     print("数组 b 的元素类型:",b.dtype)
     print("数组 b 的元素个数:",b.size)

def draw():
    x = np.linspace(0,2*np.pi,100)
    y_sin = np.sin(x)
    y_cos = np.cos(x)

    plt.figure(figsize=(8,5))

    plt.plot(x,y_sin,label="sin(x)",color="blue",linestyle="-")
    plt.plot(x,y_cos,label="cos(x)",color="red",linestyle="--")

    plt.legend()
    plt.show()

np_test()
draw()




